package com.base_data_handler.ak_xnradar;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.OptionTag;
import org.htmlparser.tags.SelectTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
/**
 * 安康单站雷达爬取
 * @param urlStr
 * @return
 */
public class AK_Radar {

	
	 static String getContent ( String urlStr ) {
	        String json = null ;
	        URL url ;
	        try {
	            url = new URL ( urlStr ) ;
	            URLConnection con = url.openConnection ( ) ;
	            con.setReadTimeout ( 1000 * 10 ) ;
	            con.setConnectTimeout ( 1000 * 10 ) ;
	            ByteArrayOutputStream outputStream = new ByteArrayOutputStream ( ) ;
	            InputStream inputStream = url.openStream ( ) ;
	            byte [ ] data = new byte [ 1024 * 8] ;
	            inputStream.read ( data ) ;
	            int len = 0 ;
	            if ( inputStream != null ) {
	                try {
	                    while ( ( len = inputStream.read ( data ) ) != - 1 ) {
	                        outputStream.write ( data , 0 , len ) ;
	                    }
	                    json = new String ( outputStream.toByteArray ( ) , "UTF-8" ) ;
	                    outputStream.close ( ) ;
	                } catch ( Exception e ) {
	                    e.printStackTrace ( ) ;
	                }
	            }
	        } catch ( Exception e ) {
	            e.printStackTrace ( ) ;
	        }
	        return json ;
	    }
//	 <img alt="" id="wxyt_img" src="http://pi.weather.com.cn/i/product/pic/l/sevp_aoc_rdcp_sldas_ebref_aswc_l88_pi_20170622091800001.png">
	   private static void parse ( String htmlStr ) {
		 //  System.out.println(htmlStr);
	        Parser parser ;
	            try {
	            	 parser = new Parser ("<html><" + htmlStr ) ;
	            	
//	            	 AndFilter filter = new AndFilter(new TagNameFilter("img"),new HasAttributeFilter("id","wxyt_img"));  
//	            	 NodeList  nodeList = parser.parse(filter);
//	            	 
//	            	 for(int j = 0; j < nodeList.size(); j++) {
//	            		  System.out.println(nodeList.elementAt(j).getText());
//				 	  }
	            	 
	            		
	            	 AndFilter filter2 = new AndFilter(new TagNameFilter("select"),new HasAttributeFilter("id","slide"));  
	            	 NodeList  nodeList2 = parser.parse(filter2);
	            	 SelectTag selectnode = (SelectTag) nodeList2.elementAt(0);
	            	 NodeList nl = selectnode.getChildren();
	            	 Node[] nl_nodes = nl.toNodeArray();
	            	 String url="";
	            	 for (int i = 0; i < nl_nodes.length; i++) {
	            		 Node optnode = (Node) nl_nodes[i];
	            		 if (optnode instanceof OptionTag) {
	            			 OptionTag opttag = (OptionTag) optnode;
	            			 url=opttag.getAttribute("bigPic");
	            		 }
					}
	            	 System.out.println(url);
	            	 
	            } catch (ParserException e) {
					e.printStackTrace();
				}
	    }
	   public static void main(String[] args) {
//		   parse( getContent("http://www.weather.com.cn/radar/index.shtml?XN"));
		   parse( getContent("http://products.weather.com.cn/product/radar/index/procode/JC_RADAR_AZ9915_JB.shtml"));
		   
	   }
}
